<template>
  <div class="home" style="overflow-y: hidden">
    <!-- 筛选 -->
    <div class="search_content">
      <div class="task_header">
        <div class="top_title">
          <div class="top_title_back" @click="onClickLeft">
            <i class="iconfont icon-back"></i>
          </div>
          <div class="top_title_val">{{ $route.meta.title }}</div>

          <div style="position: absolute;right: 10px;color: #fff;font-size: 14px;line-height: 50px;top: 0" @click="addCar">新增车辆</div>
        </div>


        <div class="top_search">
          <div class="top_search_val">
            <form action>
              <van-search v-model="codeNo" ref="searchCodeNo" style="height: 55px" placeholder="请输入车架号或者车牌号" shape="round" background="#FFFFFF" @search="searchData" @clear="clearSearch" />
            </form>
          </div>
        </div>
      </div>

      <div style="width: 100%;text-align: center;padding-bottom: 10px;background-color: #F2F2F2">
        <van-button @click="searchData" type="info" style="width: 95%;height: 40px;border-radius: 4px;background-color: #3975c6">查询</van-button>
      </div>
    </div>

    <div style="margin-top: 20px;font-size: 13px;padding: 0px 10px 0px 10px" v-if="recentSearchList.length>0">
      <div style="display: flex;justify-content: space-between;align-items: center;margin-bottom: 10px">
        <div style="color: rgba(120,120,120,0.6)">最近搜索</div>
        <div @click="clearRecentSearch"><van-icon name="delete-o" color="rgba(120,120,120,0.6)" size="15px"/></div>
      </div>
      <div style="margin-top: 10px;display: flex;flex-flow: row wrap;align-items: center">
        <div v-for="(item, index) in recentSearchList" :key="index" style="margin: 5px">
          <div style="width: max-content;background-color: rgba(120,120,120,0.2);padding: 8px 13px;border-radius: 20px;color: rgba(120,120,120,0.9)"><span @click="searchItem(item)" >{{ item }}</span><span style="color: rgba(255,255,255,1);margin-left: 5px" @click="deleteSearch(index)">✖</span></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

import { Dialog } from 'vant'

export default {
  name: 'carOrderSearch',
  components: {
  },
  data() {
    return {
      codeNo: '',
      recentSearchList: [],
    }
  },
  mounted() {
  },
  activated() {
    this.$nextTick(() => {
      this.codeNo = ''
      this.recentSearchList = localStorage.getItem('recentSearchList') ? JSON.parse(localStorage.getItem('recentSearchList')) : [],
      this.$refs.searchCodeNo && this.$refs.searchCodeNo.focus()
    });
  },

  methods: {
    // 初始化列表
    searchData() {
      if (!!this.codeNo && this.codeNo.trim() != '') {
        this.pushRecentSearchList(this.codeNo)
        this.$router.push({
          path: '/carOrderList',
          query: { codeNo: this.codeNo },
        })
      }
    },

    clearSearch() {
      this.codeNo = ''
    },
    onClickLeft() {
      this.$router.push('/home')
    },
    pushRecentSearchList(item) {
      this.recentSearchList = this.recentSearchList.filter((i) => i != item)
      this.recentSearchList.unshift(item)
      this.recentSearchList = this.recentSearchList.slice(0, 20)
      localStorage.setItem('recentSearchList', JSON.stringify(this.recentSearchList))
    },
    deleteSearch(index) {
      Dialog.confirm(
        { message: '确定删除该搜索记录吗？' }
      ).then(() => {
        this.recentSearchList.splice(index, 1)
        localStorage.setItem('recentSearchList', JSON.stringify(this.recentSearchList))
      })

    },
    searchItem(item) {
      this.codeNo = item
    },
    clearRecentSearch() {
      Dialog.confirm(
        { message: '确定清空搜索记录吗？' }
      ).then(() => {
        this.recentSearchList = []
        localStorage.setItem('recentSearchList', JSON.stringify(this.recentSearchList))
      })

    },
    addCar() {
      this.$router.push('/addCar')
    }
  },
}
</script>
<style>
html,
body {
  font-size: 50px !important;
  background: #fff;
  box-sizing: border-box;
}
</style>
<style lang="less" scoped="">
.home {
  height: 100%;
  background: #f2f2f2;
  //font-family: 'Alibaba';
}

.search_content {
  //position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 2;
}


.task_header {
  // background: url('~@/assets/images/title_bg.png') no-repeat center;
  // height: 2.6rem;
  //height: 1.6rem;
  background: url('~@/assets/images/taskbg.png') no-repeat center;
  font-size: 0.24rem;
  background-size: 100% 100%;

  .top_title {
    position: relative;
    width: 100%;
    height: 50px;

    .top_title_back {
      position: absolute;
      left: 10px;
      line-height: 50px;
      color: #fff;
    }

    .top_title_val {
      text-align: center;
      line-height: 50px;
      color: #fff;
      font-weight: bold;
      font-size: 16px;
    }
    .onlyPerson {
      position: absolute;
      right: 10px;
      color: #fff;
      top: 13px;
      clear: both;
      .onlyPerson_txt {
        font-size: 12px;
        color: #fff;
        margin-bottom: 5px;
      }
      .onlyPerson_switch {
        float: right;
      }
    }
  }

  .task_header_data {
    position: relative;
    .top_arrow {
      background: rgba(255, 255, 255, 0.7);
      border-radius: 50%;
      width: 20px;
      height: 20px;
      line-height: 20px;
      text-align: center;
      color: #3975c6;
      opacity: 0.4;
    }
    .top_left {
      position: absolute;
      top: 22px;
      left: 10px;
      z-index: 3;
    }
    .top_right {
      position: absolute;
      top: 22px;
      right: 10px;
      z-index: 3;
    }
    .task_header_date {
      display: flex;
      align-items: center;
      width: 80%;
      margin: auto;
      margin-bottom: 10px;

      .task_header_date_w {
        width: 30px;
      }

      .task_header_date_item {
        width: calc((100% - 30px) / 5);
        text-align: center;
        color: #fff;
        font-size: 14px;
      }
    }

    .task_header_followData {
      display: flex;
      align-items: center;
      color: #fff;
      width: 80%;
      margin: auto;
      margin-bottom: 10px;

      .task_header_followData_tt {
        width: 30px;
        text-align: center;
      }

      .task_header_followData_item {
        width: calc((100% - 30px) / 5);
        text-align: center;
        font-size: 12px;
        font-weight: bold;
        &.active {
          position: relative;
          &::after {
            content: '';
            width: 30px;
            height: 2px;
            position: absolute;
            bottom: -7px;
            left: 50%;
            margin-left: -15px;
            background: #fff;
          }
          // border-bottom: 1px solid #fff;
        }
      }
    }
  }
}
.top_search {
  display: flex;
  align-items: center;
  background: #f2f2f2;
  width: 100%;
  justify-content: space-between;

  .top_search_val {
    width: 100%;
  }

  .top_search_icon {
    width: 120px;
    position: relative;
    top: 2px;

    img {
      width: 100%;
    }
  }
}


</style>
